﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Implement;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Holiday;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Holiday
{
    public class ShowHolidaysCommandHarder : IRequestHandler<ShowHolidaysCommand, APIPanging<HolidaysDto>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRepository<Holidays> repository;
        private readonly IBaseRepository<User> baseRepository;
        private readonly IBaseRepository<WF_Recode> @base;

        public ShowHolidaysCommandHarder(IMapper mapper, IBaseRepository<Holidays> repository, IBaseRepository<User> baseRepository, IBaseRepository<WF_Recode> @base)
        {
            this.mapper = mapper;
            this.repository = repository;
            this.baseRepository = baseRepository;
            this.@base = @base;
        }

        public async Task<APIPanging<HolidaysDto>> Handle(ShowHolidaysCommand request, CancellationToken cancellationToken)
        {
            APIPanging<HolidaysDto> result = new APIPanging<HolidaysDto>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();

            var kk = baseRepository.GetAll().Result.Where(x => x.UserName == request.UserName).FirstOrDefault();


            var list = from a in repository.GetAll().Result
                       join b in baseRepository.GetAll().Result on a.Bmld equals b.UserId 


                       select new HolidaysDto
                       {
                           HoId = a.HoId,
                           UserId = b.UserId,
                           UserName = b.UserName,
                           HoName = a.HoName,

                           DepId = a.DepId,

                           HolbType = a.HolbType,

                           HoTime = a.HoTime,

                           TimeCount = a.TimeCount,

                           BdaoTime = a.BdaoTime,

                           Holyue = a.Holyue,
                           Bmld = a.Bmld,
                           Bmname = b.UserName,
                           Spzt = a.Spzt,

                           CreateTime = a.CreateTime,

                           Del = a.Del,

                           approvalId = b.approvalId - 1,
                        yj=a.yj,

                       };


            if (request.HoTime != null )
            {
                list = list.Where(x => x.HoTime >= request.HoTime.Value );
            }
            if (request.BdaoTime != null && request.BdaoTime != null)
            {
                list = list.Where(x => x.HoTime >= request.HoTime.Value && x.BdaoTime <= request.BdaoTime.Value);
            }

            if (request.Spzt != null)
            {
                list = list.Where(x => x.Spzt == request.Spzt);
            }
            if (!string.IsNullOrEmpty(request.HoName))
            {
                list = list.Where(x => x.HoName.Contains(request.HoName));
            }
            if (kk.approvalId == 0)
            {
                list = list.Where(x => x.HoName == kk.UserName);
            }
            else {
                list = list.Where(x => x.DepId == kk.DepId);
            }

            

            int totalcount = list.Count();
            int totalpage = (int)Math.Ceiling(totalcount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.HoId).Skip((request.Pageindex - 1) * request.PageSize).Take(request.PageSize);
            result.TotalPage = totalpage;
            result.TotalCount = totalcount;
            result.Code = APIEnum.查询成功;
            result.Msg = "分页查询成功";
            result.List = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
